package com.lemeng.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.StringUtil;
import com.lemeng.dao.SpecMapper;
import com.lemeng.dao.TemplateMapper;
import com.lemeng.entity.PageResult;
import com.lemeng.pojo.Spec;
import com.lemeng.pojo.Template;
import com.lemeng.service.SpecService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Map;

@Service(interfaceClass = SpecService.class)
public class SpecServiceImpl implements SpecService {

    @Autowired
    SpecMapper specMapper;

    @Override
    public List<Spec> findAll() {
        return specMapper.selectAll();
    }

    @Override
    public PageResult<Spec> findPage(Integer page, Integer size) {
        PageHelper.startPage(page,size);
        PageInfo<Spec> specPageInfo = new PageInfo<>(specMapper.selectAll());
//        Page<Spec> specs = (Page<Spec>)specMapper.selectAll();
        return new PageResult(specPageInfo.getTotal(),specPageInfo.getList());
    }

    @Override
    public List<Spec> findList(Map<String, Object> serchMap) {
        Example example = new Example(Spec.class);
//        example.setOrderByClause(" template_id desc ");

        Example.Criteria criteria = example.createCriteria();
        if (serchMap != null) {
            if (serchMap.get("name")!=null && StringUtil.isNotEmpty(serchMap.get("name").toString())){
                criteria.andLike("name","%"+serchMap.get("name").toString()+"%");
            }
            if (serchMap.get("templateId")!=null && StringUtil.isNotEmpty(serchMap.get("templateId").toString())){
                criteria.andEqualTo("templateId",serchMap.get("templateId").toString());
            }
        }
        return specMapper.selectByExample(example);
    }

    @Override
    public PageResult<Spec> findPage(Integer page, Integer size, Map<String, Object> serchMap) {
        PageHelper.startPage(page,size);
        PageInfo<Spec> specPageInfo = new PageInfo<>(findList(serchMap));
        return new PageResult(specPageInfo.getTotal(),specPageInfo.getList());
    }

    @Override
    public Spec findById(Integer id) {
        return specMapper.selectByPrimaryKey(id);
    }

    @Autowired
    TemplateMapper templateMapper;

    @Override
    @Transactional
    public void add(Spec spec) {
        specMapper.insert(spec);
        Template template = templateMapper.selectByPrimaryKey(spec.getTemplateId());
        template.setSpecNum(template.getSpecNum()+1);
        templateMapper.updateByPrimaryKeySelective(template);
    }

    @Override
    public void update(Spec spec) {
        specMapper.updateByPrimaryKeySelective(spec);
    }

    @Override
    @Transactional
    public void delete(Integer id) {
        Spec spec = specMapper.selectByPrimaryKey(id);
        specMapper.deleteByPrimaryKey(id);
        Template template = templateMapper.selectByPrimaryKey(spec.getTemplateId());
        template.setSpecNum(template.getSpecNum()-1);
        templateMapper.updateByPrimaryKeySelective(template);
    }


}
